package com.goktech.olala.core.service.impl;

import com.goktech.olala.core.resp.RespBrandVo;
import com.goktech.olala.core.resp.RespCategoryVo;
import com.goktech.olala.core.service.ICategoryService;
import com.goktech.olala.server.dao.goods.BrandMapper;
import com.goktech.olala.server.dao.goods.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname CategoryServiceImp
 * @Description TODO
 * @Date 2020/12/3 1:31
 * @Created by sanming
 */
@Service
public class CategoryServiceImp implements ICategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    BrandMapper brandMapper;

    @Override
    public List<RespCategoryVo> slectCategory() {
        List<RespCategoryVo> firstList = categoryMapper.slectCategory();

        List<RespCategoryVo> secondList = null;

        List<RespCategoryVo> threeList = null;

        for (RespCategoryVo first : firstList) {
            String categoryIdArr = first.getCategoryIdArr();
            String[] split = categoryIdArr.split(",");
            secondList = new ArrayList<>();
            for (String cateId : split) {
                Integer cate = Integer.valueOf(cateId);
                List<RespCategoryVo> id = categoryMapper.selectByA(cate);
                if (id != null) {
                    for (RespCategoryVo second : id) {
                        Integer categoryId1 = second.getCategoryId();
                        threeList = categoryMapper.selectByA(categoryId1);
                        second.setChildList(threeList);
                    }
                    secondList.addAll(id);
                }
            }
                first.setChildList(secondList);
            }
            return firstList;
        }

    @Override
    public List<RespBrandVo> selectBrand() {
        List<RespBrandVo> respBrandVos = brandMapper.selectBrand();
        return respBrandVos;
    }
}
